package com.block.api.controller.source;

import com.alibaba.fastjson.JSONObject;
import com.block.common.model.vo.ResultInfoVO;
import com.block.db.entity.ApplicationInfo;
import com.block.db.entity.ChannelBaseInfo;
import com.block.db.entity.SourceBaseInfo;
import com.block.source.model.param.UserDataParam;
import com.block.source.service.ISourceCheckUserService;
import com.block.source.service.ISourceRegisterUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 线索 url  对接
 * 撞库返回用户的授权链接
 *
 * 需要通过拦截器进行处理
 */
@RestController
@RequestMapping("apisource/source")
public class SourceController {

    @Autowired
    ISourceCheckUserService sourceCheckUserService;
    @Autowired
    ISourceRegisterUserService sourceRegisterUserService;

    @RequestMapping(value = "/checkUser", method = {RequestMethod.POST})
    public String checkUser(@RequestAttribute(value = "tranData") JSONObject tranData,
                             @RequestAttribute(value = "applicationInfo") ApplicationInfo applicationInfo,
                             @RequestAttribute(value = "channelBaseInfo") ChannelBaseInfo channelBaseInfo,
                             @RequestAttribute(value = "sourceBaseInfo") SourceBaseInfo sourceBaseInfo,
                             @RequestAttribute(value = "tranDataBean") UserDataParam userDataParam,
                             @RequestAttribute(value = "uri") String uri,
                             @RequestAttribute(value = "ip") String ip) {

        userDataParam.setRequestIp(ip);

        ResultInfoVO resultInfoVO = sourceCheckUserService.sourceCheckUser1(sourceBaseInfo, userDataParam, channelBaseInfo, applicationInfo);
        return resultInfoVO.toResp();
    }

    @RequestMapping(value = "/registerUser", method = {RequestMethod.POST})
    public String registerUser(@RequestAttribute(value = "tranData") JSONObject tranData,
                             @RequestAttribute(value = "applicationInfo") ApplicationInfo applicationInfo,
                             @RequestAttribute(value = "channelBaseInfo") ChannelBaseInfo channelBaseInfo,
                               @RequestAttribute(value = "sourceBaseInfo") SourceBaseInfo sourceBaseInfo,
                               @RequestAttribute(value = "tranDataBean") UserDataParam userDataParam,
                             @RequestAttribute(value = "uri") String uri,
                             @RequestAttribute(value = "ip") String ip) {

        userDataParam.setRequestIp(ip);

        String resultNo = userDataParam.getOrderNo();
        ResultInfoVO resultInfoVO = sourceRegisterUserService.sourceRegisterUser1(resultNo, sourceBaseInfo, userDataParam);
        return resultInfoVO.toResp();
    }

}